package com.wy.music_player;

import com.wy.adapter.DBAdapter;
import com.wy.domain.User;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;


public class Sign extends Activity {
	// log.i
	private String TAG ="Sign";
	// UI OBJECT
	EditText ed_username,ed_password;
	Spinner sp_country;
	RadioButton rb_female,rb_male;
	RadioGroup rg_sex;
	Button sign;
	//test btn
	Button del;
	// SharePreferences
	SharedPreferences.Editor editor;
	// database
	private DBAdapter dbAdapter = null;
	private String username,password,sex,country;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_layout);
        
      	bindViews();
//        editor = getSharedPreferences("data",MODE_PRIVATE).edit();

		// getCountry
		sp_country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent,View view,int position,long id){
				country = parent.getItemAtPosition(position).toString();
				Toast.makeText(Sign.this, "success country = " + country, Toast.LENGTH_LONG).show();
			}
			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				country = parent.getItemAtPosition(0).toString();
				Toast.makeText(Sign.this, "success country = " + country, Toast.LENGTH_LONG).show();
			}
		});
		// getSex
		rg_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
				Log.i(TAG, "onCheckedChanged: checkedId = " + checkedId+";female = "+rb_female.getId());
				if (checkedId == rb_female.getId())
					sex = "female";
				else if (checkedId == rb_male.getId())
					sex = "male";
				else
					sex = "";
				Log.i(TAG, "onCheckedChanged: sex = " + sex);
			}
		});
		// sign
        sign.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// userInfo
				username = ed_username.getText().toString();
				password = ed_password.getText().toString();
				country = sp_country.getSelectedItem().toString();
				Log.i(TAG, "onClick: sign : username="+username+";password="+password+";country = "+country+";sex = " + sex);
				// check info
				if(!username.equals("") && !password.equals("") && !country.equals("") && !sex.equals("")){
					if(dbAdapter == null){
						dbAdapter = new DBAdapter(Sign.this);
						dbAdapter.open();
					}else{
						dbAdapter.open();
					}
					// fill in data
					User user = new User();
					user.setUsername(username);
					user.setPassword(password);
					user.setSex(sex);
					user.setCountry(country);
					long flag = dbAdapter.insert(user);
					Log.i(TAG, "onClick: sign:user insert flag = "+flag);

					// return login
					Intent it = new Intent();
					it.putExtra("username",username);
					it.putExtra("password",password);
                    setResult(2,it);
					finish();
				}else{
					Toast.makeText(Sign.this, "error:name or password or country or sex cannot be empty", Toast.LENGTH_SHORT).show();
				}
			}
		});

		// test delete all user
		del.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dbAdapter.deleteAll();
			}
		});
    }
    private void bindViews(){
		ed_username = (EditText) findViewById(R.id.username);
		ed_password = (EditText) findViewById(R.id.password);
		sp_country = (Spinner) findViewById(R.id.country);
		rg_sex = (RadioGroup) findViewById(R.id.sex);
		rb_female = (RadioButton) findViewById(R.id.female);
		rb_male = (RadioButton) findViewById(R.id.male);
		sign = (Button) findViewById(R.id.sign);
		del = (Button) findViewById(R.id.del);

		rg_sex.clearCheck();
	}
}
